#define _CRT_SECURE_NO_WARNINGS 1
#include"List.h"
//初始化链表
LTNode* LTInit()
{
	//用创建节点函数来进行初始化哨兵位
	LTNode* head = BuyLTNode(0);
	//最开始，是哨兵位的自循环
	head->prev = head;
	head->next = head;
	//返回哨兵位
	return head;
}
//新节点的创建
LTNode* BuyLTNode(LTDataType x)
{
	//用malloc函数开辟一个节点大小的空间给新节点
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	//将新节点的各项数组进行初始化
	newnode->data = x;
	newnode->next = NULL;
	newnode->prev = NULL;

	//返回新节点
	return newnode;
}
//链表的打印
void LTPrint(LTNode* phead)
{
	//断言
	assert(phead);
	printf("phead<=>");
	//从哨兵位的下一个进行遍历打印
	LTNode* head = phead->next;
	while (head != phead)
	{
		printf("%d<=>", head->data);
		head = head->next;
	}
	printf("\n");
}
//尾插
void LTPushBack(LTNode* phead, LTDataType x)
{
	//断言
	assert(phead);
	//新节点的创建
	LTNode* newnode = BuyLTNode(x);
	//得到尾节点
	LTNode* tail = phead->prev;
	//进行尾节点和尾插节点的循环
	tail->next = newnode;
	newnode->prev = tail;
	//进行尾插节点和哨兵的循环
	newnode->next = phead;
	phead->prev = newnode;

}
//尾删
void LTPopBack(LTNode* phead)
{
	//断言
	assert(phead);
	//断言防止只有哨兵位
	assert(phead->next != phead);
	//得到尾节点和尾节点的前一个节点
	LTNode* tail = phead->prev;
	LTNode* tailprev = tail->prev;
	//释放尾节点
	free(tail);
	//进行tailprev和哨兵的循环
	tailprev->next = phead;
	phead->prev = tailprev;
}
//头插
void LTPushFront(LTNode* phead, LTDataType x)
{
	//断言
	assert(phead);
	//新节点的创建
	LTNode* newnode = BuyLTNode(x);
	//得到首节点
	LTNode* first = phead->next;
	//进行首节点和头插节点的循环
	newnode->next = first;
	first->prev = newnode;
	//进行头插节点和哨兵的循环
	phead->next = newnode;
	newnode->prev = phead;
}

//头删
void LTPopFront(LTNode* phead)
{
	//断言
	assert(phead);
	//断言防止只有哨兵位
	assert(phead->next != phead);
	//得到头结点和第二个节点
	LTNode* first = phead->next;
	LTNode* second = phead->next->next;
	//进行第二个节点和哨兵的循环
	second->prev = phead;
	phead->next = second;
	//释放头结点
	free(first);
}
//计算节点个数
int LTSize(LTNode* phead)
{
	//断言
	assert(phead);
	int size = 0;
	//从哨兵位的下一个节点开始计数
	LTNode* cur = phead->next;
	while (cur != phead)
	{
		cur = cur->next;
		size++;
	}
	return size;
}
//查找值为x的节点
LTNode* LTFind(LTNode* phead, LTDataType x)
{
	//断言
	assert(phead);
	//从哨兵位的下一个节点开始遍历寻找
	LTNode* cur = phead->next;
	while (cur->next != phead)
	{
		if (cur->data == x)
			return cur;
		cur = cur->next;
	}
	return NULL;
}
//在pos节点前插入一个值为x的节点
void LTInsert(LTNode* pos, LTDataType x)
{
	//断言
	assert(pos);
	//得到pos的前一个节点
	LTNode* posprev = pos->prev;
	//新节点的创建
	LTNode* newnode = BuyLTNode(x);
	//进行新节点和pos的循环
	newnode->next = pos;
	pos->prev = newnode;
	//进行新节点和pos节点前一个节点的循环
	newnode->prev = posprev;
	posprev->next = newnode;
}
//删除pos节点
void LTErase(LTNode* pos)
{
	//断言
	assert(pos);
	//得到pos节点的前后节点
	LTNode* posnext = pos->next;
	LTNode* posprev = pos->prev;
	//进行pos前后节点的循环
	posprev->next = posnext;
	posnext->prev = posprev;
	//释放掉pos节点
	free(pos);
}
//销毁链表
void LTDestroy(LTNode* phead)
{
	//断言
	assert(phead);
	//从哨兵位的下一个节点开始free
	LTNode* cur = phead->next;
	while (cur != phead)
	{
		LTNode* next = cur->next;
		free(cur);
		cur = cur->next;
	}
	//最后free哨兵位
	free(phead);
}